package com.base.helper;

import com.alibaba.fastjson.JSON;
import com.base.controller.utils.bank.HttpClientUtils;
import com.base.controller.utils.bank.RsaUtils;

import java.util.HashMap;
import java.util.Map;

public class BankHelper {

    /**
     * 生产地址
     */
    private static final String baseHost = "https://lion-cv.51ley.com/apis";

    /**
     * 站点id
     */
    private static final Long oemID = 997053907714703360L;

    /**
     * 商户号
     */
    private static final String merchant = "conversion-test";

    /**
     * 公钥
     */
    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDeKKRgL8BqMb6vQzjaDPwOzIMFInPsBnx0ydxvEaJj+sRI1l5jQG7NiOWK9XcA3/1i92DW4/hJlbkqALfQj7ISRfAz" +
            "SVSbFcnkQ25CllBx/RRZ/dj/fhh6pG/o1obASE+/f+MqLQJ8bXjdmm/rUKASHCtz9xghHS+FB5JqQje6QIDAQAB";

    /**
     * 私钥
     */
    private static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIN4opGAvwGoxvq9DONoM/A7MgwUic+wGfHTJ3G8RomP6xEjWXmNAbs2I5Yr1dwDf/WL3YNbj+EmVuSo" +
            "At9CPshJF8DNJVJsVyeRDbkKWUHH9FFn92P9+GHqkb+jWhsBIT79/4yotAnxteN2ab+tQoBIcK3P3GCEdL4UHkmpCN7pAgMBAAECgYA/6shuoP872UhT4tF/RcvqgX0Fbn7JoD4c2aGVpAlPG88W1+7KL4ml8N5AR" +
            "jOE8ZamVEdvc3Zx6efY5u3uiRBoLWRKyYmAwldmqaAaNbjgLFrWaVB0To9hUlHocwKSXCMfU3lWnyCCY9mPchpSMeO8yUVy7Q4HjqtFlDBuuMCbWQJBAN/TX+m3q+dvpvIrvRZxNOr5GelokZ6x939yw6guYh1/j5" +
            "CI8yCHbcMsHIIpaBilr/DtLyoDyBnRaAh5mGaeVtMCQQCWXq62n/Hq5RV+k8rUkAYB/zIgQZyg1x7lKFB7cZzXsj2vLm6G2PUKyM5+SdpNdU9gq9fYLvqgYhe/x7M5+BXTAkB/FOqOLqTB4NjvAvp3NYuQJiMaLS8" +
            "Q5Ybaq/R/FubKTNTcgYVZYMtnVzJQjOX3qIs+xcxTlJYWYpkoxrFLAnfVAkA/wCqgYJVG+hKv1GbU0a7KX/BXfpmyEyIVE49zlophZD8Eq1HOt+LDzLjHD/aOimK54IElNV2qa4wYEclplUVjAkAIzJArENUEl+FC" +
            "URdOtf6aopJvEr2FE/Qoa1gJdLq71I1NTto4EC8+s/EEBVPUlK+J7byhRpugrDROirTIJrFE";



    /**
     * 保存网申
     */
    public static String cardSave(String mobile, String name, String idCard, String stationChannelId, String callbackUrl, String clientNo) {
        String url = baseHost + "/credit/open/apply/card/save";
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);
        //建立请求参数
        Map<String, Object> map = new HashMap<>();
        //申请人手机号
        map.put("mobile", mobile);
        //申请人姓名
        map.put("name", name);
        //申请人身份证号
        map.put("idCard", idCard);
        //通道id  网申列表中的的stationChannelId
        map.put("stationChannelId", stationChannelId);
        //回调地址
        map.put("callbackUrl", "www.baidu.com");
        //客户端编号，唯一请求标识
        map.put("clientNo", clientNo);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));
        //建立post请求，传递json格式参数
        String resultText = HttpClientUtils.doPost(url,baseMap, JSON.toJSONString(map));
        System.out.println(resultText);

        return resultText;
    }

    /**
     * 卡种通道详情
     */
    public static String cardChannelById() {
        String url = baseHost + "/credit/open/apply/card/1017699186700713985"; //{通道id}
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));
        //建立GET请求
        String resultText = HttpClientUtils.doGet(url,baseMap);
        System.out.println(resultText);

        return  resultText;
    }

    /**
     * 卡通道
     */
    public static String cardChannel() {
        String url = baseHost + "/credit/open/apply/card/channel";
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));

        Map<String,Object> map = new HashMap<>();
        String json = JSON.toJSONString(map);
        //建立post请求
        String resultText = HttpClientUtils.doPost(url,baseMap,json);
        System.out.println(resultText);

        return resultText;
    }

    /**
     * 标签
     */
    public static String cardTag() {
        String url = baseHost + "/credit/open/apply/card/tag";
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));
        //建立GET请求
        String resultText = HttpClientUtils.doGet(url,baseMap);
        System.out.println(resultText);

        return resultText;
    }

    /**
     * 网申主题
     */
    public static String cardTopic() {
        String url = baseHost + "/credit/open/apply/card/topic";
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));
        //建立GET请求
        String resultText = HttpClientUtils.doGet(url,baseMap);
        System.out.println(resultText);

        return resultText;
    }

    /**
     * 网申银行列表
     */
    public static String cardBank() {
        String url = baseHost + "/credit/open/apply/card/bank";
        //生成时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        //公钥加密
        String sign = RsaUtils.encrypt(publicKey, timestamp);
        //建立请求头部数据
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("X-Auth-OEM",oemID.toString());
        baseMap.put("X-Open-Sign",sign);
        baseMap.put("X-Open-Merchant",merchant);
        baseMap.put("X-Open-Timestamp",timestamp);

        System.out.println("timestamp:" + timestamp);
        System.out.println("sign:" + sign);
        System.out.println("解析结果:" + RsaUtils.decrypt(privateKey, sign));
        //建立GET请求
        String resultText = HttpClientUtils.doGet(url,baseMap);
        System.out.println(resultText);

        return resultText;
    }
}
